package com.yh.correct.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.yh.correct.model.ResultData;
import com.yh.correct.model.SysPermission;
import com.yh.correct.model.vo.Tree;
import com.yh.correct.service.ISysPermissionService;

/**
 * 权限管理控制层
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value="/permission")
public class SysPermissionController {
	
	//日志
	private static Logger logger = LogManager.getLogger(SysPermissionController.class);

	//权限服务层接口
	@Autowired
	private ISysPermissionService permissionService;
	
	/**
	 * 跳转到权限管理页面
	 * @return
	 */
	@RequestMapping(value="/show")
	public ModelAndView toPermissionPage(){
		ModelAndView mv=new ModelAndView("/system/syspermission");
		List<Tree> list=null;
		try {
			list=permissionService.findTreeList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mv.addObject("perList", list);
		return mv;
	}
	
	/**
	 * 动态模糊查询权限列表
	 * @param name
	 * @param url
	 * @param type
	 * @param parent
	 * @return
	 */
	@RequestMapping(value="/search")
	public ResultData searchPermission(String name,String url,String type,String pid,Integer pageNum,Integer pageSize){
		Map<String,Object> condition=new HashMap<String,Object>();
		condition.put("name", name);
		condition.put("url", url);
		condition.put("type", type);
		condition.put("pid", pid);
		condition.put("pageNum", pageNum);
		condition.put("pageSize", pageSize);
		PageInfo<Object> pageInfo=null;
		try {
			pageInfo=permissionService.findListByCondition(condition);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Object> pageList=new ArrayList<Object>();
		pageList.add(pageInfo);
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(pageList);
		return rd;
	}
	
	/**
	 * 跳转到增加权限页面
	 * @return
	 */
	@RequestMapping(value="/toAdd")
	public ModelAndView toAddPermission(){
		ModelAndView mv=new ModelAndView("/system/syspermission_add");
		List<Tree> list=null;
		try {
			list=permissionService.findTreeList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mv.addObject("perList", list);
		return mv;
	}
	
	
	/**
	 * 增加权限
	 * @param permission
	 * @return
	 */
	@RequestMapping(value="/doAdd")
	public ResultData doAddPermission(SysPermission permission){
		int i=0;
		try {
			i=permissionService.insertSysPermission(permission);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("增加成功！");
		}else{
			rd.setCode(4001);
			rd.setMessage("增加失败！");
		}
		return rd;
	}
	
	
	/**
	 * 删除权限
	 * @param permission
	 * @return
	 */
	@RequestMapping(value="/doDel")
	public ResultData doDelPermission(SysPermission permission){
		int i=0;
		try {
			i=permissionService.deleteSysPermission(permission);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("增加成功！");
		}else{
			rd.setCode(4001);
			rd.setMessage("增加失败！");
		}
		return rd;
	}
	
	/**
	 * 跳转到修改权限页面
	 * @param permission
	 * @return
	 */
	@RequestMapping(value="/toEdit")
	public ModelAndView toEditPermission(SysPermission permission){
		SysPermission sysPermission=null;
		List<Tree> list=null;
		try {
			sysPermission=permissionService.getSysPermission(permission);
			list=permissionService.findTreeList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ModelAndView mv=new ModelAndView("/system/syspermission_edit");
		mv.addObject("permission", sysPermission);
		mv.addObject("perList", list);
		return mv;
	}
	
	/**
	 * 修改权限
	 * @param permission
	 * @return
	 */
	@RequestMapping(value="/doEdit")
	public ResultData doEditPermission(SysPermission permission){
		int i=0;
		try {
			i=permissionService.updateSysPermission(permission);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("修改成功！");
		}else{
			rd.setCode(4001);
			rd.setMessage("修改失败！");
		}
		return rd;
	}
	
	/**
	 * 跳转到权限明细页面
	 * @param permission
	 * @return
	 */
	@RequestMapping(value="/detail")
	public ModelAndView toDetailPermission(SysPermission permission){
		SysPermission sysPermission=null;
		try {
			sysPermission=permissionService.getSysPermission(permission);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ModelAndView mv=new ModelAndView("/system/syspermission_detail");
		mv.addObject("permission", sysPermission);
		return mv;
	}
	
	/**
	 * 获取权限tree结构
	 * @return
	 */
	@RequestMapping(value="/getTree")
	public ResultData getPermission(){
		List<Tree> list=null;
		try {
			list=permissionService.findTreeList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(list!=null){
			rd.setCode(0);
			rd.setMessage("获取成功！");
			rd.setData(list);
		}else{
			rd.setCode(4001);
			rd.setMessage("获取失败！");
		}
		return rd;
	}
}
